<?php
class mTrigger{

    private static $callable = array();
    private static $object = array();

    private function __construct(){

    }

    /**
     * @param string $event
     * @param callable $callable
     * @return mixed
     */
    public static function register($event,$callable){
        if(empty($event)) return FALSE;

        if(is_string($callable) && strpos($callable ,'/')){//处理('main/index')这种回调
            list($class, $method) = explode('/', $callable);
            if(!in_array($class, array_keys(self::$object))) self::$object[$class] = new $class();
            $callable = array(self::$object[$class], $method);
        }
        if(!is_callable($callable)) return FALSE;


        if(!is_array(self::$callable[$event])) self::$callable[$event] = array();
        return array_push(self::$callable[$event], $callable);
    }


    /**
     * @param $event
     * @param $callable
     * @return boolean
     */
    public static function unRegister($event, $callable){
        if(in_array($event, array_keys(self::$callable[$callable]))){
            unset(self::$callable[$event][$callable]);
            return TRUE;
        }else{
            return FALSE;
        }
    }

    /**
     * @param $event
     * @return bool
     */
    public static function trigger($event){
        if(!in_array($event,array_keys(self::$callable))) return FALSE;
        array_walk(self::$callable[$event], function($key){
            call_user_func($key);
        });
        return TRUE;
    }
}
